package com.wzxy.pf.budget.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzxy.pf.budget.entity.Budget;
import com.wzxy.pf.budget.mapper.BudgetMapper;
import com.wzxy.pf.budget.service.BudgetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhl
 * @since 2023-04-01
 */
@Service
public class BudgetServiceImpl extends ServiceImpl<BudgetMapper, Budget> implements BudgetService {
@Resource
 BudgetMapper budgetMapper;
    @Override
    public  IPage findByuId(Integer cruPage, Integer pageSize, Integer uId) {
        IPage<Budget> budgetPage=new Page<>(cruPage,pageSize);
        budgetMapper.findByuid(budgetPage,uId);
        return budgetPage;
    }

    @Override
    public IPage page(Integer cruPage, Integer pageSize, Integer uId) {
     IPage page=findByuId(cruPage,  pageSize,  uId);
        return page;
    }
    @Transactional
    @Override
    public int delete(List<String> ids) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        String updatetime=formatter.format(date);
        Budget budget=new Budget();
      for(String i:ids){
          budget.setId(Integer.valueOf(i));
          budget.setUpdateTime(updatetime);
          budgetMapper.update(budget);
          System.out.println( budgetMapper.update(budget));
      }
        return budgetMapper.deleteBatchIds(ids);


    }

    @Override
    public int saveBudget(Budget budget) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        String createTiime=formatter.format(date);
        budget.setCreateTime(createTiime);
        budget.setIsDeleted(0);
        return budgetMapper.insert(budget);

    }

    @Override
    public int updateBudget(Budget budget) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));
        String updatetime=formatter.format(date);
        budget.setUpdateTime(updatetime);
        return budgetMapper.updateById(budget);

    }

    @Override
    public List<Budget> search(String time) {
        return budgetMapper.search(time);
    }
}
